import NativeClient, { NativeClientIns } from "./NativeClient";


/** 原生端行为模拟 */
class VirtualClient extends NativeClientIns {
	private static response_cache: Map<string, [abd.$planet_NudityPromise_poof$<any>, any]> = new Map();
	private static used_token: string[] = [];

	public callStaticMethod(api: string, data: any, ccdata?: any){
		return VirtualClient.request(api, data, ccdata);
	}

	private static request($planet_api_poof$: string, $planet_data_poof$: any, ccdata?: any){

		let token = abd.$planet_string_poof$.$planet_randomToken_poof$(5, 36, t=>!VirtualClient.used_token.includes(t)).toUpperCase();
		let np = new abd.$planet_NudityPromise_poof$();
		VirtualClient.response_cache.set(token, [np, ccdata]);

		let $planet_request_poof$ = {
			$planet_cc_token_poof$: token,
			$planet_data_poof$
		};
		VirtualClient.$planet_virtualFunc_poof$($planet_api_poof$, JSON.stringify($planet_request_poof$));
		return {$planet_api_poof$, $planet_request_poof$, $planet_response_poof$: np.$planet_promise_poof$};
	}

	private static response(api: string, json_string: string){
		let response = JSON.parse(json_string);
		let token = response.$planet_cc_token_poof$;
		let [np, ccdata] = VirtualClient.response_cache.get(token);
		VirtualClient.response_cache.delete(token);
		response.$planet_cc_token_poof$ = ccdata ?? token;

		np.$planet_resolve_poof$(response);
	}

	/** 原生端功能模拟 */
	private static $planet_virtualFunc_poof$(api: string, json_string: string){
		if(!Reflect.has(VirtualClient, api)) return void 0;

		let request = JSON.parse(json_string);
		let response: any = Reflect.apply(Reflect.get(VirtualClient, api), VirtualClient, [Object.assign({}, request)]);

		if(response) VirtualClient.response(api, JSON.stringify(response));
		else VirtualClient.response_cache.delete(request.$planet_cc_token_poof$)
	}

	private static $planet_setRESData_poof$(response: any, data: any){
		Reflect.set(response, '$planet_data_poof$', data);
	}

	/** 模拟广告行为 */
	private static $planet_AD_poof$(response: any){
		VirtualClient.$planet_setRESData_poof$(response, {
			$planet_status_poof$: 'ad-over',
		});
		return response;
	}

	/** 模拟本地化行为 */
	private static $planet_LocalLanguage_poof$(response: any){
		VirtualClient.$planet_setRESData_poof$(response, {
			$planet_countryCode_poof$: 'CN',
			$planet_languageCode_poof$: 'zh',
		});
		return response;
	}

	/** 模拟链接行为 */
	private static $planet_Link_poof$(response: any){
		window.open(response.$planet_data_poof$.url);
		return undefined;
	}

	/** 模拟获取设备ID */
	private static $planet_device_id_poof$(response: any){
		let virtual_device_ID = localStorage.getItem('VIRTUAL-DEVICE-ID');
		if(!virtual_device_ID){
			virtual_device_ID = [8, 4, 4, 4, 12].map(len=>abd.$planet_string_poof$.$planet_random_poof$(len, 16).toUpperCase()).join('-');
			virtual_device_ID = '<chrome-virtual>['+virtual_device_ID+']';
			localStorage.setItem('VIRTUAL-DEVICE-ID', virtual_device_ID);
		}
		VirtualClient.$planet_setRESData_poof$(response, {
			$planet_device_id_poof$: virtual_device_ID,
		});
		return response;
	}

	/** 模拟奖励领取 */
	private static $planet_fruit_poof$(){
		return undefined;
	}

	/** 模拟弹窗浮层 */
	private static $planet_float_poof$(){
		return undefined;
	}

	private static $planet_set_clipboard_poof$(response: any){
		abd.$planet_log_poof$.$planet_tag_poof$('设置剪贴板: green;', response.$planet_data_poof$.$planet_text_poof$);
		return undefined;
	}

	private static $planet_game_type_poof$(response: any){
		VirtualClient.$planet_setRESData_poof$(response, {
			$planet_type_poof$: 'B',
		});
		return response;
	}
}

//NativeClient.initClient(new VirtualClient());